package com.linewell.system.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linewell.system.dao.ISysCustomerDao;
import com.linewell.system.model.SysCustomer;
import com.linewell.system.model.SysDepartment;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.service.ISysCustomerService;
import com.linewell.system.utils.HqlHelper;

/**
 * 实现SysCustomer接口方法 
 */
@Service
public class SysCustomerServiceImpl implements ISysCustomerService {

	@Autowired
	private ISysCustomerDao sysCustomerDao;

	@Override
	public boolean save(SysCustomer sysCustomer) {
		// SessionFactory sessionFactory
		sysCustomerDao.save(sysCustomer);
		return true;
	}

	@Override
	public List<SysCustomer> findAll() {
		// TODO Auto-generated method stub
		return sysCustomerDao.find("FROM SysCustomer");
	}

	@Override
	public DataGrid datagrid(PageHelper ph, SysCustomer c, Integer depId) {
		DataGrid dg = new DataGrid();
		List<SysCustomer> cml = new ArrayList<SysCustomer>();
		// 用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();

		// 注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer("from SysCustomer t where 1=1 ");

		// //以下为查询条件

		if (null != c.getSysDepartment() && c.getSysDepartment().getDepId() != 0) {
			params.put("depId", c.getSysDepartment().getDepId());
			hql.append(" and t.sysDepartment.depId = :depId ");
		}
		if (c.getCustName() != null && !"".equals(c.getCustName())) {
			String custName = null;
			try {
				custName = URLDecoder.decode(c.getCustName(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			params.put("custName", "%" + custName + "%");
			hql.append(" and t.custName like :custName ");
		}
		if (c.getPhone() != null && !"".equals(c.getPhone())) {
			params.put("phone", "%" + c.getPhone() + "%");
			hql.append(" and t.phone like :phone ");
		}
		if (c.getSex() != null && !"".equals(c.getSex())) {
			params.put("sex", c.getSex());
			hql.append(" and t.sex = :sex ");
		}
		if (c.getStatus() != null && !"".equals(c.getStatus())) {
			params.put("status", c.getStatus().byteValue());
			hql.append(" and t.status = :status ");
		}

		if (depId != null && !"".equals(depId)) {
			hql.append("  AND FIND_IN_SET(t.sysDepartment.depId,getChildDepId(" + depId + ")) > 0 ");
		}

		hql = HqlHelper.orderHql(hql, ph); // 排序
		List<SysCustomer> l = sysCustomerDao.find(hql.toString(), params, ph.getPage(), ph.getRows());
		dg.setRows(l);
		dg.setTotal(sysCustomerDao.count("select count(*) " + hql, params));
		return dg;

	}

	@Override
	public boolean removeAll(String ids) {
		// TODO Auto-generated method stub
		sysCustomerDao.removeAll(ids);
		return false;
	}

	@Override
	public boolean deleteAll(String ids) {
		sysCustomerDao.deleteAll(ids);
		return false;
	}

	@Override
	public List<SysCustomer> getUser(String id) {
		String hql = "from SysCustomer s where s.custId=" + id;
		List<SysCustomer> c = sysCustomerDao.find(hql);
		return c;
	}

	@Override
	public boolean update(SysCustomer sysCustomer) {
		sysCustomerDao.updateAll(sysCustomer);
		return false;
	}

	@Override
	public List<SysDepartment> getDep() {
		return sysCustomerDao.getDep();
	}

	@Override
	public boolean saveOrUpdate(SysCustomer sysCustomer) {
		sysCustomerDao.saveOrUpdate(sysCustomer);
		return true;

	}

	@Override
	public boolean deleteByOpenId(String fromUserName) {
		String hql = "delete SysCustomer s where s.openId = '" + fromUserName + "' ";
		sysCustomerDao.executeHql(hql);
		return true;
	}

	@Override
	public List<SysCustomer> getUserByOpenId(String fromUserName) {
		String hql = "from SysCustomer s where s.openId='" + fromUserName + "'";
		List<SysCustomer> c = sysCustomerDao.find(hql);
		return c;
	}

	@Override
	public boolean saveUpdate(SysCustomer c, String fromUserName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void updateSubCribe(String fromUserName) {
		// TODO Auto-generated method stub
		
	}

}
